package com.ruoyi.project.bajiaostar.user;


import com.ruoyi.common.utils.IpUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.user.domain.TUser;
import com.ruoyi.project.user.entity.LoginByMobileReq;
import com.ruoyi.project.user.service.ITUserService;
import com.ruoyi.project.utils.RedisUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 用户端登陆验证
 *
 */
@RestController
@RequestMapping("/nt/user")
public class WebUserController extends BaseController {

    @Autowired
    ITUserService userService;


    @ApiOperation("登录")
    @Log(title = "APP接口管理", businessType = BusinessType.OTHER)
    @PostMapping("/login")
    public AjaxResult loginByMobile (LoginByMobileReq req, HttpServletRequest request) {
        try {
            if(ObjectUtils.isEmpty(req)){
                return AjaxResult.error("信息有误,请检查");
            }
            req.setLastLoginIp(IpUtils.getIpAddr(request));
            Map<String, Object> map = this.userService.loginByMobile(req);
            return AjaxResult.success(map);
        } catch (Exception e) {
            return this.error("登录失败 " + e.getMessage());
        }
    }


    @ApiOperation("修改头像信息")
    @Log(title = "APP接口管理", businessType = BusinessType.OTHER)
    @PostMapping("/updateUser")
    public AjaxResult updateUser (TUser req) {
        try {

            if(ObjectUtils.isEmpty(req)){
                return AjaxResult.error("信息有误,请检查");
            }
            Long appUserId = getAppUserId();
            if(ObjectUtils.isEmpty(appUserId)){
                return AjaxResult.error("未登录,请检查是否登陆");
            }
            req.setId(appUserId);
            int i = this.userService.updateTUser(req);
            if(i < 1 ){
                return AjaxResult.error("修改失败,请检查");
            }
            return AjaxResult.success();
        } catch (Exception e) {
            return this.error("修改失败 " + e.getMessage());
        }
    }
}
